home *** CD-ROM | disk | FTP | other *** search
/ isnet Internet / Isnet Internet CD.iso / prog / hiz / 09 / 09.exe / adynware.exe / perl / lib / site / HTTP / Request.pm < prev    next >
Encoding:
Perl POD Document  |  1999-12-28  |  3.2 KB  |  135 lines

  1.  
  2. package HTTP::Request;
  3.  
  4. =head1 NAME
  5.  
  6. HTTP::Request - Class encapsulating HTTP Requests
  7.  
  8. =head1 SYNOPSIS
  9.  
  10.  require HTTP::Request;
  11.  $request = new HTTP::Request 'GET', 'http://www.oslonett.no/';
  12.  
  13. =head1 DESCRIPTION
  14.  
  15. C<HTTP::Request> is a class encapsulating HTTP style requests,
  16. consisting of a request line, a MIME header, and optional
  17. content. Note that the LWP library also uses this HTTP style requests
  18. for non-HTTP protocols.
  19.  
  20. Instances of this class are usually passed to the C<request()> method
  21. of an C<LWP::UserAgent> object:
  22.  
  23.  $ua = new LWP::UserAgent;
  24.  $request = new HTTP::Request 'http://www.oslonett.no/';
  25.  $response = $ua->request($request);
  26.  
  27. =head1 METHODS
  28.  
  29. C<HTTP::Request> is a subclass of C<HTTP::Message> and therefore
  30. inherits its methods.  The inherited methods are header(),
  31. push_header(), remove_header(), headers_as_string() and content().
  32. See L<HTTP::Message> for details.
  33.  
  34. =cut
  35.  
  36. require HTTP::Message;
  37. @ISA = qw(HTTP::Message);
  38.  
  39. use URI::URL ();
  40. use strict;
  41.  
  42. =head2 $r = new HTTP::Request $method, $url, [$header, [$content]]
  43.  
  44. Constructs a new C<HTTP::Request> object describing a request on the
  45. object C<$url> using method C<$method>.  The C<$url> argument can be
  46. either a string, or a reference to a C<URI::URL> object.  The $header
  47. argument should be a reference to a HTTP::Headers object.
  48.  
  49.  $request = new HTTP::Request 'GET', 'http://www.oslonett.no/';
  50.  
  51. =cut
  52.  
  53. sub new
  54. {
  55.     my($class, $method, $url, $header, $content) = @_;
  56.     my $self = bless new HTTP::Message $header, $content;
  57.     $self->method($method);
  58.     $self->url($url);
  59.     $self;
  60. }
  61.  
  62.  
  63. sub clone
  64. {
  65.     my $self = shift;
  66.     my $clone = bless $self->HTTP::Message::clone;
  67.     $clone->method($self->method);
  68.     $clone->url($self->url);
  69.     $clone;
  70. }
  71.  
  72.  
  73. =head2 $r->method([$val])
  74.  
  75. =head2 $r->url([$val])
  76.  
  77. These methods provide public access to the member variables containing
  78. respectively the method of the request and the URL object of the
  79. request.
  80.  
  81. If an argument is given the member variable is given that as its new
  82. value. If no argument is given the value is not touched. In either
  83. case the previous value is returned.
  84.  
  85. The url() method accept both a reference to a URI::URL object and a
  86. string as its argument.  If a string is given, then it should be
  87. parseable as an absolute URL.
  88.  
  89. =cut
  90.  
  91. sub method  { shift->_elem('_method', @_); }
  92.  
  93. sub url
  94. {
  95.     my $self = shift;
  96.     my($url) = @_;
  97.     if (@_) {
  98.     if (!defined $url) {
  99.     } elsif (ref $url) {
  100.         $url = $url->abs;
  101.     } else {
  102.         eval {  $url = URI::URL->new($url); };
  103.         $url = undef if $@;
  104.     }
  105.     }
  106.     $self->_elem('_url', $url);
  107. }
  108.  
  109. *uri = \&url;  # this is the same for now
  110.  
  111. =head2 $r->as_string()
  112.  
  113. Method returning a textual representation of the request.
  114. Mainly useful for debugging purposes. It takes no arguments.
  115.  
  116. =cut
  117.  
  118. sub as_string
  119. {
  120.     my $self = shift;
  121.     my @result = ("--- $self ---");
  122.     my $url = $self->url;
  123.     $url = (defined $url) ? $url->as_string : "[NO URL]";
  124.     push(@result, $self->method . " $url");
  125.     push(@result, $self->headers_as_string);
  126.     my $content = $self->content;
  127.     if ($content) {
  128.     push(@result, $self->content);
  129.     }
  130.     push(@result, ("-" x 35));
  131.     join("\n", @result, "");
  132. }
  133.  
  134. 1;
  135.